﻿namespace NetEnvironmentVariable ; 

    public static class JavaEV {
        /// <summary>
        /// 设置Java的环境变量
        /// </summary>
        /// <param name="rootPath">JDK安装目录</param>
        public static void Set(string rootPath) {
            //1.判断当前目录是否有 bin 目录
            var rtPath = Path.GetFullPath(rootPath).Replace(@"\\", @"\");
            string pathExists = Path.Combine(rtPath, "bin");
            if (Directory.Exists(pathExists) == false) {
                Console.WriteLine("请设置正确的JDK目录!");
                return;
            }
            
            //2.在 Path 中 添加 "..\bin" 和 "..\jre\bin"
            EV.SetByKey("Path", @$"{rtPath}\bin;{rtPath}\jre\bin");
            //3.设置 CLASSPATH
            EV.SetByKey("CLASSPATH", @$".;{rtPath}\lib\dt.jar;{rtPath}\lib\tools.jar;");
            Console.WriteLine("Java环境配置完成!");
        }
    }